package com.ydd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ydd.constant.MessageConstant;
import com.ydd.dto.YddArticleDto;
import com.ydd.dto.YddArticlePageDto;
import com.ydd.context.BaseContext;
import com.ydd.context.SysBaseContext;
import com.ydd.dto.*;
import com.ydd.entity.YddArticle;
import com.ydd.entity.YddUser;
import com.ydd.entity.YddUserData;
import com.ydd.mapper.YddArticleMapper;
import com.ydd.result.Result;
import com.ydd.mapper.YddUserDataMapper;
import com.ydd.mapper.YddUserMapper;
import com.ydd.result.Result;
import com.ydd.service.YddArticleService;
import com.ydd.vo.YddCateVO;
import com.ydd.vo.PageVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhangxiaojun
 * @since 2023-05-14
 */
@Service
public class YddArticleServiceImpl extends ServiceImpl<YddArticleMapper, YddArticle> implements YddArticleService {
    @Autowired
    private YddUserDataMapper dataMapper;

    /**
     * 查看我的文章
     *
     * @param dto
     * @return
     */
    @Override
    public Result myArticlePage(MyArticlePageDTO dto) {

        String currentId = SysBaseContext.getCurrentId();
        IPage<YddArticle> page = new Page<>(dto.getPage(),dto.getLimit());
        LambdaQueryWrapper<YddArticle> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(YddArticle::getUserId, currentId);
        page(page, wrapper);
        PageVO vo = PageVO.builder()
                .count((int) page.getTotal())
                .data(page.getRecords())
                .totalPages((int) page.getPages())
                .pageSize((int) page.getSize())
                .currentPage((int) page.getCurrent())
                .build();
        return Result.success(vo);
    }

    /**
     * 根据id查看文章
     *
     * @param id
     * @return
     */
    @Override
    public Result selectByArticleId(Long id) {

        YddArticle article = getById(id);
        return Result.success(article);
    }

    /**
     * 修改文章
     *
     * @param dto
     * @return
     */
    @Override
    public Result editArticle(EditArticleDTO dto) {
        YddArticle article = new YddArticle();
        BeanUtils.copyProperties(dto, article);
        LambdaQueryWrapper<YddArticle> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(YddArticle::getId, dto.getId());
        update(article, wrapper);
        return Result.success();
    }

    /**
     * 点赞/取消点赞和收藏/取消收藏
     *
     * @param dto
     * @return
     */
    @Override
    public Result userDataHandle(UserDataHandleDTO dto) {
        String uId = SysBaseContext.getCurrentId();
        Integer ui= Integer.valueOf(uId);
        YddUserData userData = YddUserData.builder()
                .userId(ui)
                .type(dto.getType())
                .articleId(dto.getArticle_id())
                .build();
        if (dto.getAction().equals("add")){
        dataMapper.insert(userData);
        }
        if (dto.getAction().equals("del")){
            LambdaQueryWrapper<YddUserData> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(YddUserData::getArticleId,dto.getArticle_id()).eq(YddUserData::getType,dto.getType());
            dataMapper.delete(wrapper);

        }

        return Result.success();

    }

    /**
     * 发布文章
     *
     * @param dto
     * @return
     */
    @Override
    public Result addArticle(AddArticleDTO dto) {

        YddArticle article=new YddArticle();
        BeanUtils.copyProperties(dto,article);
        article.setUserId(Integer.valueOf(SysBaseContext.getCurrentId()));
        save(article);
        return Result.success();
    }

    /**
     * 删除我的文章
     *
     * @return
     */
    @Override
    public Result delArticle(Long id) {
        LambdaQueryWrapper<YddArticle> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(YddArticle::getId,id);
        remove(wrapper);
        return Result.success();
    }

    @Autowired
    private YddArticleMapper yddArticleMapper;

    /**
     * 添加文章
     *
     * @param yddArticle
     * @return
     */
    @Override
    public boolean addArticle(YddArticle yddArticle) {
        return yddArticleMapper.insert(yddArticle) > 0;
    }

    /**
     * 删除文章
     *
     * @param id
     * @return
     */
    @Override
    public boolean deleteById(Long id) {
        return yddArticleMapper.deleteById(id) > 0;
    }

    /**
     * 文章分类详情请
     *
     * @param id
     * @return
     */
    @Override
    public YddArticleDto getCateById(Long id) {
        YddArticle yddArticle = yddArticleMapper.selectById(id);
        YddArticleDto yddArticleDto = new YddArticleDto();
        BeanUtils.copyProperties(yddArticle,yddArticleDto);
        return yddArticleDto;
    }

    /**
     * 修改文章
     *
     * @param yddArticle
     * @return
     */
    @Override
    public boolean updateArticle(YddArticle yddArticle) {
        int rows = yddArticleMapper.updateById(yddArticle);
        return rows > 0;
    }


    /**
     * 分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public Result YddArticleList(YddArticlePageDto dto) {
        if (dto == null){
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }
        IPage<YddArticle> page = new Page<>(dto.getPage(),dto.getLimit());

        LambdaQueryWrapper<YddArticle> wrapper = new LambdaQueryWrapper<>();

        wrapper.like(YddArticle::getTitle,dto.getTitle()).or().like(YddArticle::getCateid,dto.getCateId())
                .lt(YddArticle::getCreateDate,dto.getStartTime()).gt(YddArticle::getUpdateDate,dto.getEndTime());

        this.page(page,wrapper);
        YddCateVO vo = YddCateVO.builder()
                .count((int)page.getTotal())
                .totalPages((int)page.getPages())
                .pageSize((int)page.getSize())
                .currentPage((int)page.getCurrent())
                .data(page.getRecords())
                .build();
        return Result.success(vo);
    }
}
